package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sl.au.entity.SinglePump;
import com.sl.au.repository.SinglePumpRepository;
import com.sl.au.service.SinglePumpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.sl.au.service.ExcelService;


@Service("singlePumpService")
@Transactional
public class SinglePumpServiceImpl extends BaseServiceImpl implements SinglePumpService {

    @Autowired
    SinglePumpRepository singlePumpRepository;

    @Autowired
    ExcelService excelService;
    
    @Override
    public List<SinglePump> getSinglePumps(int start, int length, String key, String superior,String dataFilter,String orderName, String dir) {
        Page<SinglePump> pages = null;
        Pageable pageable = null; 
        if(dataFilter.equals("所有")){
        	pageable=new PageRequest(start/length, length, Sort.Direction.DESC, "type","superior");
        	pages = singlePumpRepository.getPumpPrice(key, pageable);
        }else {
        	pageable=new PageRequest(start/length, length, Sort.Direction.DESC,"createTime");
        	pages = singlePumpRepository.getPumpPrice(superior,key, pageable);
        }
        return pages.getContent();
    }

    public SinglePumpRepository getRepository() {
        return this.singlePumpRepository;
    }

    @SuppressWarnings("unchecked")
    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public long importExcel(String fileName, String superior) {
        long count=0;
        if (!StringUtils.hasText(fileName))
            return count;
        Map<String, Object> collections = new HashMap<>();
        //excelService.setStorageLocation(CRM.getStorageLocation());
        excelService.importExcel("priceofsinglePump", collections,fileName);
        if (collections.size() < 1 || collections.get("items") == null)
            return count;
        List<SinglePump> rows = (List<SinglePump>) collections.get("items");
        List<SinglePump> prices=this.getChecked(rows, superior);
        if (null!=prices && prices.size() > 0) {
            this.getRepository().save(prices);
            count=prices.size();
        }
        return count;
    }

	@Override
	public List<SinglePump> getChecked(List<SinglePump> rows,String superior) {
		List<SinglePump> singlePumps=new ArrayList<SinglePump>();
		for(SinglePump s:rows){
			String type=s.getType();
			SinglePump p=this.getRepository().findByTypeAndSuperior(type, superior);
			if(p!=null){
				p.setPrice(s.getPrice());
				singlePumps.add(p);	
			}	
		}
		return singlePumps;
	}   
}
